/*
 *
 *
 *  Copyright (C) Microsoft Corporation.  All rights reserved
 *
 *
 */

#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

#endregion

namespace CRMScenarios
{
	partial class ActionsPane : UserControl
	{

		AttachDoc attDoc = null; //  ew AttachDoc();
		InsertFlds insFld = null; // ew InsertFlds();
        int scenario = 0; // stands for nothing.
        object ActiveOne = null;

		public ActionsPane()
		{
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.FromArgb(0, 0, 0, 0);
            this.UpdateStyles();
            this.SizeChanged += new EventHandler(ActionsPane_SizeChanged);
			attDoc = new AttachDoc();
			insFld = new InsertFlds();
            this.AutoScroll = true;
		}

        void ActionsPane_SizeChanged(object sender, EventArgs e)
        {
            this.Refresh();
            if (this.Width < 181)
            {
                insFld.sizeChange(180, this.Size.Height);
                attDoc.sizeChange(180, this.Size.Height);
                this.Refresh();
                return;
            }
            //this.HScroll = this.VScroll = false;
            insFld.sizeChange(this.Size.Width, this.Size.Height);
            attDoc.sizeChange(this.Size.Width, this.Size.Height);
            this.Refresh();
        }

		public void changeScenario(int scenarioSelection, string entityType, string searchPattern, ref BusinessEntity be, RefreshOption refresh)
		{
            switch (scenarioSelection)
            {
                case 1:
                    insFld.RefreshAccordingToTheOtherPane(entityType, searchPattern, ref be, refresh);
                    insFld.Refresh();
                    this.Controls.Clear(); // Remove(attDoc);
                    this.Controls.Add(insFld);
                    break;
                case 2:
                    attDoc.RefreshAccordingToTheOtherPane(entityType, searchPattern, ref be, refresh);
                    attDoc.Refresh();
                    this.Controls.Clear(); //.Remove(insFld);
                    this.Controls.Add(attDoc);
                    break;

                default:
                    insFld.RefreshAccordingToTheOtherPane(entityType, searchPattern, ref be, refresh);
                    insFld.Refresh();
                    this.Controls.Clear(); // Remove(attDoc);
                    scenarioSelection = 1; // for insert field.
                    this.Controls.Add(insFld);
                    break;
            }

            scenario = scenarioSelection;
			ActionsPane_SizeChanged(null, null);
		}

        private void ActionsPane_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        public void ShutDownSetScenario()
        {
            OfficeApi.SetCustomProperty(ActiveOne, scenario.ToString(), "CRMVSTO");
        }

        public void InitializeControls(object application, object activeOne)
        {
            // LOGIN HERE?
            this.Show();
            Application.DoEvents();

            ActiveOne = activeOne;
            object objScenarioSelection = OfficeApi.GetCustomProperty(ActiveOne, "CRMVSTO");
            int scenarioSelection = scenario;
            if (objScenarioSelection != null)
            {
                scenarioSelection = Convert.ToInt32((string)objScenarioSelection);
            }
            BusinessEntity dummy = null;
            changeScenario(scenarioSelection, string.Empty, string.Empty, ref dummy, RefreshOption.Initial);

            string TargetServerURL = CacheManager.GerServerURL(string.Empty);
            bool login = CrmConnector.Login(TargetServerURL);
            if (login == false)
            {
                attDoc.FlagErrorOrWarning(global::CRMScenarios.Resources.LoginFailure, InfoOptions.Error);
                insFld.FlagErrorOrWarning(global::CRMScenarios.Resources.LoginFailure, InfoOptions.Error);
                return;
            }

            insFld.InitializeControls(application, activeOne);
            attDoc.InitializeControls(application, activeOne);

        }
	}
}
